import axios from "axios";
import QS from "qs"; // 引入qs模块，用来序列化post类型的数据

//定义异常返回信息
const error = {
  code: 0,
  msg: "请求异常",
  data: {}
};

export async function request(url, data, type) {
  try {
    //url="http://localhost:8002" +url;
    url="http://49.123.21.101:8761" +url;
    //url = "http://218.75.221.254:38761" + url;
    // url = "http://10.36.31.157:38761" + url;
    // // 判断请求类型，部署到对方公司云平台时，使用下面这个获取token
    if (type === "get") {
      return (await axios.get(url, { params: data ,headers: {"token":
              localStorage.getItem("token") }})).data;
    } else if (type === "post") {
      console.log(QS.stringify(data));
      return (
          await axios.post(url, data, {
            headers: {"token":
                  localStorage.getItem("token") },})
      ).data;
      }else if (type === "post1") {
          console.log(QS.stringify(data));
          return (
              await axios.post(url, data, {
                headers: {"token":
                      localStorage.getItem("token") },})
          );
        } else if(type === "get1") {
          return (await axios.get(url, {headers: {
              "token":localStorage.getItem("token")
            }}))   }
        else if(type === "put") {
          return (await axios.put(url, {headers: {
              "token":localStorage.getItem("token")
            }}))   }
        else if(type === "delete") {
          return (await axios.delete(url, {headers: {
              "token":localStorage.getItem("token")
            }}))   }
        else {
          return error;
        }
      } catch (e) {
        console.log(e);
        return error;
      }
    }
 //    //本地部署测试时使用这个，token已经是写好了的
 //    if (type === "get") {
 //      return (await axios.get(url, { params: data ,headers: {
 //          "token":"eyJUeXBlIjoiSnd0IiwidHlwIjoiSldUIiwiYWxnIjoiSFMyNTYifQ.eyJ0b2tlbiI6IntcImNoaW5lc2VOYW1lXCI6XCLnrqHnkIblkZhcIixcImNsaWVudElkXCI6XCJjb21cIixcImlkXCI6MSxcImp0aVwiOlwiMzU1MTdhYmQtYjBiZi00OGQ3LTllNmMtMTYwMDI2Y2FkYmRiXCIsXCJtZW1iZXJOYW1lXCI6XCJhZG1pblwiLFwibW9iaWxlXCI6XCIxMzkyMDk5MTcxMVwifSJ9.CGg1jux6iAuQRXXmBa5NTjJCuCzqL3kGZGXpZI4whIw"
 //        }})).data;
 //    } else if (type === "post") {
 //      console.log(QS.stringify(data));
 //      return (
 //          await axios.post(url, data, {
 //            headers: {"Content-Type":
 //                  "multipart/form-data","token":"eyJUeXBlIjoiSnd0IiwidHlwIjoiSldUIiwiYWxnIjoiSFMyNTYifQ.eyJ0b2tlbiI6IntcImNoaW5lc2VOYW1lXCI6XCLnrqHnkIblkZhcIixcImNsaWVudElkXCI6XCJjb21cIixcImlkXCI6MSxcImp0aVwiOlwiMzU1MTdhYmQtYjBiZi00OGQ3LTllNmMtMTYwMDI2Y2FkYmRiXCIsXCJtZW1iZXJOYW1lXCI6XCJhZG1pblwiLFwibW9iaWxlXCI6XCIxMzkyMDk5MTcxMVwifSJ9.CGg1jux6iAuQRXXmBa5NTjJCuCzqL3kGZGXpZI4whIw" },})
 //      ).data;
 //    }else if (type === "post1") {
 //      console.log(QS.stringify(data));
 //      return (
 //          await axios.post(url, data, {
 //            headers: {"token": "eyJUeXBlIjoiSnd0IiwidHlwIjoiSldUIiwiYWxnIjoiSFMyNTYifQ.eyJ0b2tlbiI6IntcImNoaW5lc2VOYW1lXCI6XCLnrqHnkIblkZhcIixcImNsaWVudElkXCI6XCJjb21cIixcImlkXCI6MSxcImp0aVwiOlwiMzU1MTdhYmQtYjBiZi00OGQ3LTllNmMtMTYwMDI2Y2FkYmRiXCIsXCJtZW1iZXJOYW1lXCI6XCJhZG1pblwiLFwibW9iaWxlXCI6XCIxMzkyMDk5MTcxMVwifSJ9.CGg1jux6iAuQRXXmBa5NTjJCuCzqL3kGZGXpZI4whIw"}})
 //      );
 //    } else if(type === "get1") {
 //      return (await axios.get(url, {headers: {
 //          "token":"eyJUeXBlIjoiSnd0IiwidHlwIjoiSldUIiwiYWxnIjoiSFMyNTYifQ.eyJ0b2tlbiI6IntcImNoaW5lc2VOYW1lXCI6XCLnrqHnkIblkZhcIixcImNsaWVudElkXCI6XCJjb21cIixcImlkXCI6MSxcImp0aVwiOlwiMzU1MTdhYmQtYjBiZi00OGQ3LTllNmMtMTYwMDI2Y2FkYmRiXCIsXCJtZW1iZXJOYW1lXCI6XCJhZG1pblwiLFwibW9iaWxlXCI6XCIxMzkyMDk5MTcxMVwifSJ9.CGg1jux6iAuQRXXmBa5NTjJCuCzqL3kGZGXpZI4whIw"
 //        }}))   }
 //    else if(type === "put") {
 //      return (await axios.put(url, {headers: {
 //          "token":"eyJUeXBlIjoiSnd0IiwidHlwIjoiSldUIiwiYWxnIjoiSFMyNTYifQ.eyJ0b2tlbiI6IntcImNoaW5lc2VOYW1lXCI6XCLnrqHnkIblkZhcIixcImNsaWVudElkXCI6XCJjb21cIixcImlkXCI6MSxcImp0aVwiOlwiMzU1MTdhYmQtYjBiZi00OGQ3LTllNmMtMTYwMDI2Y2FkYmRiXCIsXCJtZW1iZXJOYW1lXCI6XCJhZG1pblwiLFwibW9iaWxlXCI6XCIxMzkyMDk5MTcxMVwifSJ9.CGg1jux6iAuQRXXmBa5NTjJCuCzqL3kGZGXpZI4whIw"
 //        }}))   }
 //    else if(type === "delete") {
 //      return (await axios.delete(url, {headers: {
 //          "token":"eyJUeXBlIjoiSnd0IiwidHlwIjoiSldUIiwiYWxnIjoiSFMyNTYifQ.eyJ0b2tlbiI6IntcImNoaW5lc2VOYW1lXCI6XCLnrqHnkIblkZhcIixcImNsaWVudElkXCI6XCJjb21cIixcImlkXCI6MSxcImp0aVwiOlwiMzU1MTdhYmQtYjBiZi00OGQ3LTllNmMtMTYwMDI2Y2FkYmRiXCIsXCJtZW1iZXJOYW1lXCI6XCJhZG1pblwiLFwibW9iaWxlXCI6XCIxMzkyMDk5MTcxMVwifSJ9.CGg1jux6iAuQRXXmBa5NTjJCuCzqL3kGZGXpZI4whIw"
 //        }}))   }
 //    else {
 //      return error;
 //    }
 //  } catch (e) {
 //    console.log(e);
 //    return error;
 //  }
 // }
